<?php

namespace app\admin\controller;

use think\Db;
use think\facade\Request;

class System extends Common
{
    /********************************站点管理*******************************/
    //站点设置
    public function system($sys_id = 1)
    {
        $table = db('system');
        if (Request::isAjax()) {
            $data = Request::except('file');
            if (false !== $table->where('id', 1)->update($data)) {
                savecache('System');

                return json(['code' => 1, 'msg' => '站点设置保存成功!', 'url' => url('system/system')]);
            } else {
                return json(['code' => 0, 'msg' => '站点设置保存失败！']);
            }
        } else {
            $system = $table->find($sys_id);
            $this->assign('system', json_encode($system, true));

            return $this->fetch();
        }
    }

    public function banner(){
        if (request()->isPost()) {
            $data = Request::except('file');
          
            $image=$data['image'];
            db('setting')->where(['key'=>'banner'])->delete();

            foreach ($image as $item){
                $init[]=[
                    "key"=>'banner',
                    "value"=>$item
                ];
              
            }

            if (false !==  db('setting')->insertall($init)) {
                return ['code' => 1, 'url' => url('index'), 'msg' => '修改成功!'];
            } else {
                return ['code' => 0, 'url' => url('index'), 'msg' => '修改失败!'];
            }
        } else {        
            $this->assign('title', '轮播图');

            $image      = db('setting')->where(['key'=>'banner'])->select();
            $image=array_column($image,'value');
           
            $this->assign('image',$image);
            return $this->fetch();
        }
    }

}
